<?php

include 'templateSys/SQL.php';
include 'templateSys/Settings.php';
include 'templateSys/manager_util.php';
include 'templateSys/Admin.php';

//loginCheck
checkUserFinal();

global $link;
$link = getLink();

//nacist historii
if (isset($_POST['id']) && isset($_POST['loadHistory'])) {
    $query = sprintf(SQL::$select_textHistory_byId, mysql_real_escape_string($_POST['id'])
    );
    $result = mysql_query($query, $link);
    while ($row = mysql_fetch_assoc($result)) {
        $data = $row['data'];
    }
    $query = sprintf(SQL::$update_text_byName_preview, mysql_real_escape_string($data), mysql_real_escape_string(getUser()->getAdminId()), mysql_real_escape_string(getUser()->getTextEditName())
    );
    $result = mysql_query($query, $link);
    loadTextFromPreview($link, getUser()->getTextEditName());
    echo parseTemplate(loadTemplate("editText"));
    mysql_close($link);
    exit();
}
//rychla editace
if (isset($_POST['save']) && isset($_POST['editor1'])) {
    savePreview($link, $_POST['editor1'], getUser()->getAdminId(), getUser()->getTextEditName());
    saveChanges($link, getUser()->getTextEditName(), getUser()->getAdminId(), getUser()->getActionEdit());
}

//ulozeni preview
if (isset($_POST['editor1']) && isset($_POST['preview'])) {
    savePreview($link, $_POST['editor1'], getUser()->getAdminId(), getUser()->getTextEditName());
    header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . getUser()->getActionEdit() . "&preview=1");
    mysql_close($link);
    exit();
}

//potvrzeni zmen
if (isset($_POST['makeChanges'])) {
    saveChanges($link, getUser()->getTextEditName(), getUser()->getAdminId(), getUser()->getActionEdit());
}

//vraceni zpet do editace
if (isset($_POST['editChanges'])) {
    loadTextFromPreview($link, getUser()->getTextEditName());
    echo parseTemplate(loadTemplate("editText"));
    saveSession();
    mysql_close($link);
    exit();
}

//zacatek editace
if (isset($_POST["name"]) && isset($_POST["page"])) {
    editStart($link, $_POST["name"], $_POST["page"]);
}
//editace z editTypeResolution
if (getuser()->issetProperty("page") && getuser()->issetProperty("templateValue")) {
    $value = getUser()->removeProperty("page");
    $name = getUser()->removeProperty("templateValue");
    editStart($link, $name, $value);
}

function editStart($link, $name, $page) {
    loadTextFromPreview($link, $name);
    echo parseTemplate(loadTemplate("editText"));
    getUser()->setTextEditName($name);
    getUser()->setActionEdit($page);
    getUser()->setProperty("sideBarResolution", "editTextResolution.php");
    saveSession();
    mysql_close($link);
    exit();
}

function loadTextFromPreview($link, $name) {
    $query = sprintf(SQL::$select_text_byName_preview, mysql_real_escape_string($name)
    );
    $result = mysql_query($query, $link);
    global $textToEdit;
    while ($row = mysql_fetch_assoc($result)) {
        $textToEdit = $row['data'];
    }
    $query = sprintf(SQL::$select_textHistory_byName, mysql_real_escape_string($name, $link)
    );
    $result = mysql_query($query);
    global $historyAll;
    $historyAll = '<table><thead><tr><td>DATE</td><td>DATA</td><td>EDITED BY</td><td>LOAD THIS HISTORY</td></thead><tbody>';
    $historyEnd = '</tbody></table>';
    while ($row = mysql_fetch_assoc($result)) {
        $historyAll = $historyAll . '<tr><td>' . $row['date'] . '</td><td>' . $row['data'] . '</td><td>' . $row['login'] . '</td><td><form method="post" action="editTextResolution.php"><input type="hidden" name="id" value="' . $row['id'] . '"> <input name="loadHistory" type="submit" value="Load this history"></form></td></tr>';
    }
    $historyAll = $historyAll . $historyEnd;
}

function savePreview($link, $editor, $adminId, $textEditName) {
    $query = sprintf(SQL::$update_text_byName_preview, mysql_real_escape_string($editor), mysql_real_escape_string($adminId), mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query, $link);    
}

function saveChanges($link, $textEditName, $adminId, $action) {
    $query = sprintf(SQL::$select_text_byName, mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $oldText = $row['data'];
    }
    $query = sprintf(SQL::$select_text_byName_preview, mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $newText = $row['data'];
    }
    $query = sprintf(SQL::$insert_textHistory, mysql_real_escape_string($textEditName), mysql_real_escape_string($oldText), mysql_real_escape_string($adminId)
    );
    $result = mysql_query($query);
    $query = sprintf(SQL::$update_text_byName_current, mysql_real_escape_string($newText), mysql_real_escape_string($adminId), mysql_real_escape_string($textEditName)
    );
    $result = mysql_query($query);
    if (getUser()->issetProperty("addingItem")) {
        getUser()->setProperty("backFromEdit", "true");

        header("location: " . Settings::$rootPath . "" . Settings::$typeResolutionArray[3]);
    } else {
        header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . $action);
    }
    getUser()->removeProperty("sideBarResolution");
    saveSession();
    mysql_close($link);
    exit();
}

mysql_close($link);
?>
